package com.example.game_level;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.Random;

import org.andengine.entity.Entity;
import org.andengine.entity.modifier.MoveModifier;
import org.andengine.entity.primitive.Rectangle;



public class BlockLayer extends Entity{
	
	GameScene scene;
	private LinkedList<Blocks> lowerblocks;
	private LinkedList<Blocks> upperblocks;
	public static BlockLayer instance;
	public int blockcount,color;
	private int block_color[];
	public Rectangle sprite;
	public Blocks b;
	StartActivity activity;
	
	
	public static BlockLayer getSharedInstance(){
		return instance;
	}
	
	public static boolean isEmpty(){
		if (instance.lowerblocks.size() == 0)
			return true;
		//return false;
		if (instance.upperblocks.size() == 0)
			return true;
		return false;
    }
	
	public static Iterator<Blocks> getLIterator() {
		
		return instance.lowerblocks.iterator();
	}
	
    public static Iterator<Blocks> getUIterator() {
		
		return instance.upperblocks.iterator();
	}
	
	public BlockLayer(){
		activity = StartActivity.getSharedInstance();
		activity.letterblock= new Letter_Blocks[7];
		activity.phy_position= new int [8];
		activity.lev=new int [7];
		upperblocks=new LinkedList();
		instance = this;
		blockcount=7;
		
		
		//activity.u_color= new Color[7];
		restart();
	}
	
	public BlockLayer(int y) {
		activity = StartActivity.getSharedInstance();
		activity.bricks_g0= new Blocks [8][7];
		activity.bricks= new Blocks [8][7];
		activity.bricks_up= new Blocks [8][7];
		lowerblocks=new LinkedList();
		block_color= new int[7];
		instance = this;
		blockcount=7;
		
		restart(y);
		
		
	}
	
	
	
	public void purge(){
		detachChildren();
		for(Blocks b: lowerblocks){
			BlockPool.sharedBlockPool().recyclePoolItem(b);
		}
		lowerblocks.clear();
	}
	
	
	
	
	public void restart(int y){
		lowerblocks.clear();
		clearEntityModifiers();
		clearUpdateHandlers();
		activity.phy_position[0]=449;
		for(int i=1;i<8;i++){
			activity.phy_position[i]=activity.phy_position[i-1]-41;
		}
		
		for(int i=0;i<8;i++){
			 for(int j=0;j<7;j++){
				 Blocks cursprite = new Blocks();
				 cursprite.sprite.setColor(1, 1, 1);
				activity.bricks_g0[i][j]=cursprite;
				
				activity.bricks[i][j]=cursprite;
			 }
			 
		 }
		
		
		for (int i = 0; i < blockcount; i++) {
			Blocks b = new Blocks();
			float finalPosX = (float) ((i % 7)*  b.sprite.getWidth()*1.015) ;
			float finalPosY = ((int) (i / 7)) * b.sprite.getHeight()*2;

			
			Random r = new Random();
			b.sprite.setPosition(r.nextInt(2) == 0 ? -b.sprite.getWidth() * 3
					: StartActivity.Camera_Width + b.sprite.getWidth() * 3,
					(r.nextInt(5) + 1) * b.sprite.getHeight());
			b.sprite.setVisible(true);
			      

			attachChild(b.sprite);
			b.sprite.registerEntityModifier(new MoveModifier(1,0,finalPosX , b.sprite.getY(),finalPosY ));
			
			activity.bricks_g0[0][i]= b;
			
			
			
			
		  
			
			
			
			
			lowerblocks.add(b);
		}
		
		
		setVisible(true);
		setPosition(3, y);
		
	}
	
	
	
	public void restart(){
		upperblocks.clear();
		clearEntityModifiers();
		clearUpdateHandlers();
		
		for(int i=0;i<7;i++){
		activity.lev1[i]=0;
		
	     }
	
		for (int i = 0; i < blockcount; i++) {
			Blocks b = new Blocks();
			float finalPosX = (float) ((i % 7)*  b.sprite.getWidth()*1.015) ;
			float finalPosY = ((int) (i / 7)) * b.sprite.getHeight()*2;

			
			Random r = new Random();
			b.sprite.setPosition(r.nextInt(2) == 0 ? -b.sprite.getWidth() * 3
					: StartActivity.Camera_Width + b.sprite.getWidth() * 3,
					(r.nextInt(5) + 1) * b.sprite.getHeight());
			b.sprite.setVisible(true);
			      

			attachChild(b.sprite);
			b.sprite.registerEntityModifier(new MoveModifier(1,0,finalPosX , b.sprite.getY(),finalPosY ));
			
				
			
		    
			switch(i){
            case 0:
               activity.u_color1=b.sprite.getColor();break;
            case 1:
            	activity.u_color2=b.sprite.getColor();break;
            case 2:
            	activity.u_color3=b.sprite.getColor();break;
            case 3:
            	activity.u_color4=b.sprite.getColor();break;	
            case 4:
            	activity.u_color5=b.sprite.getColor();break;	
            case 5:
            	activity.u_color6=b.sprite.getColor();break;
            case 6:
            	activity.u_color7=b.sprite.getColor();break;
            }
			
			
			
			//upperblocks.add(b);
		}
		setVisible(true);
		setPosition(3, 60);
		
		
		
		letter();
		
		
	}
	
	
		
	public void letter(){
		for (int i = 0; i < 7; i++){
			Letter_Blocks l = new Letter_Blocks();
			
			
			float finalPosX = (float) ((i % 7)*  l.sprite.getWidth()*1.015) ;
	//		float finalPosY = ((int) (i / 7)) * l.sprite.getHeight()*2;
			//color=(blocks.get(i).getcolor());
			//l.sprite.setColor(0, 0, 1);
			
			Random r = new Random();
			l.sprite.setPosition(r.nextInt(2) == 0 ? -l.sprite.getWidth() * 3
					: StartActivity.Camera_Width + l.sprite.getWidth() * 3,
					(r.nextInt(5) + 1) * l.sprite.getHeight());
			l.sprite.setVisible(true);
			      

			attachChild(l.sprite);
			l.sprite.registerEntityModifier(new MoveModifier(1,0,finalPosX , l.sprite.getY(), 60));
			activity.letterblock[i]=l;
			
		}
		setVisible(true);
		//setPosition(3, 73);
		
		
	} 
	
	
	
	public static void purgeAndRestart() {
		
		instance.purge();
		instance.restart();
	}

	@Override
	public void onDetached() {
		purge();
		clearUpdateHandlers();
		super.onDetached();
	}
	
}